<?php
declare(strict_types=1);

namespace Service\MemberService;

use Service\BaseService;
use Service\Code\Normal;
use App\Model\BbcModel\Member;
use App\Model\BbcModel\MemberInfo;
use App\Model\BbcModel\Mnuser;
use App\Model\BbcModel\ShoppingGuide;
use App\Model\BbcModel\MemberGuideLog;
use Service\AuthService\IdCardAuthService;
use Service\HelperService\Common;
use Service\AreaService\AreaService;
use Service\CacheService\CacheProviderService as Cache;
use Service\VenderClient\Hashids\Application as HashIds;
use Service\ConfigureService\ChannelConfigService;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Database\Model\Collection;

/**
 * TO C/B SBBC用户基础信息
 * @author hj
 */
class MemberBasicService extends BaseService {
    //每页条数
    protected $page = 1;
    //每页条数
    protected $limit = 10;

    /**
     * 个人资料
     * @param array $param 包含了系统默认参数
     * @author: hj
     */
    public function memberInfo(array $param) {
        if (!isset($param['mid']) || empty($param['mid']) || (preg_match("/^[1-9]\d*$/", (string)$param['mid']) != 1)) {
            return Normal::returnCode(Normal::FAIL, 'invalid argument mid');
        }
        $member = MemberInfo::where('mid', (int)($param['mid']))->select(['avatar', 'nickname', 'sex', 'mid'])->first();
        if (!$member) {
            return Normal::returnCode(Normal::FAIL, '用户不存在');
        }
        $result = [
            'avatar'   => $member->avatar,
            'nickname' => $member->nickname,
            'sex'      => $member->sex,
            'code'     => HashIds::instance()->encode($member->mid)
        ];
        return Normal::returnCode(Normal::SUC, '获取成功', $result);
    }

    /**
     * 修改用户信息
     * @param array $param 包含了系统默认参数
     * @author: hj
     */
    public function editInfo(array $param) {
        if (!isset($param['mid']) || empty($param['mid']) || (preg_match("/^[1-9]\d*$/", (string)$param['mid']) != 1)) {
            return Normal::returnCode(Normal::FAIL, 'invalid argument mid');
        }
        if (!isset($param['nickname']) || empty($param['nickname'])) {
            return Normal::returnCode(Normal::FAIL, 'invalid argument nickname');
        }
        if (!isset($param['avatar']) || empty($param['avatar'])) {
            return Normal::returnCode(Normal::FAIL, 'invalid argument avatar');
        }
        $member = MemberInfo::where('mid', (int)($param['mid']))->select(['mid', 'nickname', 'avatar'])->first();
        if (!$member) {
            return Normal::returnCode(Normal::FAIL, '用户不存在');
        }
        $member->nickname = $param['nickname'];
        $member->avatar   = $param['avatar'];
        $result           = $member->save();
        if (!$result) {
            return Normal::returnCode(Normal::FAIL, '修改失败');
        }
        return Normal::returnCode(Normal::SUC, '修改成功');
    }

    /**
     * 实名认证
     * @param array $param 包含了系统默认参数
     * @author: hj
     */
    public function editAuth(array $param) {
        if (!isset($param['mid']) || empty($param['mid']) || (preg_match("/^[1-9]\d*$/", (string)$param['mid']) != 1)) {
            return Normal::returnCode(Normal::FAIL, 'invalid argument mid');
        }
        if (!isset($param['true_name']) || empty($param['true_name'])) {
            return Normal::returnCode(Normal::FAIL, 'invalid argument true_name');
        }
        if (!isset($param['idcard']) || empty($param['idcard'])) {
            return Normal::returnCode(Normal::FAIL, 'invalid argument idcard');
        }
        if (!isset($param['cid']) || empty($param['cid'])) {
            return Normal::returnCode(Normal::FAIL, 'invalid argument cid');
        }
        $member = MemberInfo::where('mid', (int)($param['mid']))->first();
        if (!$member) {
            return Normal::returnCode(Normal::FAIL, '用户不存在');
        }
        $param['idcard'] = strtoupper($param['idcard']);
        $idcardauth      = IdCardAuthService::instance()->checkIdCard(["true_name" => $param['true_name'], "idcard" => $param['idcard'], 'cid' => $param['cid']]);
        if ($idcardauth['code'] != Normal::SUC) {
            return $idcardauth;
        }
        if (!isset($param['mobile']) || empty($param['mobile'])) {
            $param['mobile'] = Member::where('mid', $param['mid'])->value('account');
        }
        $member->truename = $param['true_name'];
        $member->idcard   = $param['idcard'];
        $member->mobile   = $param['mobile'];
        if (isset($param['card_front']) && !empty($param['card_front'])) {
            $member->card_front = $param['card_front'];
        }
        if (isset($param['card_back']) && !empty($param['card_back'])) {
            $member->card_back = $param['card_back'];
        }
        $result = $member->save();
        if (!$result) {
            return Normal::returnCode(Normal::FAIL, '认证失败');
        }
        return Normal::returnCode(Normal::SUC, '认证成功');
    }

    /**
     * 获取实名信息
     * @param array $param 包含了系统默认参数
     * @author: hj
     */
    public function authInfo(array $param) {
        if (!isset($param['mid']) || empty($param['mid']) || (preg_match("/^[1-9]\d*$/", (string)$param['mid']) != 1)) {
            return Normal::returnCode(Normal::FAIL, 'invalid argument mid');
        }
        $fields = ['truename','mobile','idcard','card_front','card_back'];
        $member = MemberInfo::where('mid', (int)($param['mid']))->select($fields)->first();
        if (!$member) {
            return Normal::returnCode(Normal::FAIL, '用户不存在');
        }
        return Normal::returnCode(Normal::SUC, '获取成功', $member->toArray());
    }

    /**
     * 绑定导购员
     * @param array $param 包含了系统默认参数
     * @author: hj
     */
    public function bindGuide(array $param) {
        if (!isset($param['mid']) || empty($param['mid']) || (preg_match("/^[1-9]\d*$/", (string)$param['mid']) != 1)) {
            return Normal::returnCode(Normal::FAIL, 'invalid argument mid');
        }
        if (!isset($param['invite_code']) || empty($param['invite_code'])) {
            return Normal::returnCode(Normal::FAIL, 'invalid argument invite_code');
        }
        if (!isset($param['mn_sid']) || empty($param['mn_sid'])) {
            return Normal::returnCode(Normal::FAIL, 'invalid argument mn_sid');
        }
        if (!isset($param['cid']) || empty($param['cid'])) {
            return Normal::returnCode(Normal::FAIL, 'invalid argument cid');
        }
        $fields = ['status', 'id', 'name'];
        $shoppingGuide=ShoppingGuide::where('code', trim($param['invite_code']))->where('mn_sid', $param['mn_sid'])->where('cid', $param['cid'])->select($fields)->first();
        if (!$shoppingGuide) {
            return Normal::returnCode(Normal::FAIL, '导购员不存在');
        }
        if($shoppingGuide->status == 0){
            return Normal::returnCode(Normal::FAIL, '导购员已禁用');
        }
        $member = MemberInfo::where('mid', (int)($param['mid']))->select(['mid','guide_now','guide_md'])->first();
        if (!$member) {
            return Normal::returnCode(Normal::FAIL, '用户不存在');
        }
        $member->guide_now   = $shoppingGuide->id;
        $member->guide_md   = time();
        $result = $member->save();
        if (!$member) {
            return Normal::returnCode(Normal::FAIL, '绑定失败');
        }
        MemberGuideLog::insert(['mid' => $param['mid'], 'guide_id' => $shoppingGuide->id, 'cd'=>time()]);
        return Normal::returnCode(Normal::SUC, '绑定成功', ['name'=>$shoppingGuide->name]);
    }

    /**
     * 查询导购员
     * @param array $param 包含了系统默认参数
     * @author: hj
     */
    public function getGuide(array $param) {
        if (!isset($param['mid']) || empty($param['mid']) || (preg_match("/^[1-9]\d*$/", (string)$param['mid']) != 1)) {
            return Normal::returnCode(Normal::FAIL, 'invalid argument mid');
        }
        $member = MemberInfo::where('mid', (int)($param['mid']))->select(['guide_now', 'mid'])->first();
        if (!$member) {
            return Normal::returnCode(Normal::FAIL, '用户不存在');
        }
        $shoppingGuide = ShoppingGuide::where('id', $member->guide_now)->select(['name', 'mn_sid'])->first();
        if (!$shoppingGuide) {
            $data['guide_name']='';
            $data['store_name']='';
        }else{
            $data['guide_name']=$shoppingGuide->name;
            $data['store_name']=Mnuser::where('id', $shoppingGuide->mn_sid)->value('store_name');
        }
        return Normal::returnCode(Normal::SUC, '获取成功', $data);



    }
}